package com.shq.ex1;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class JiShiQiActivity extends AppCompatActivity {

    private Chronometer chronometer = null;
    private Button reset = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ji_shi_qi);

        reset = (Button)findViewById(R.id.reset);
        chronometer  =(Chronometer)findViewById(R.id.chronometerKJ);

        chronometer.setBase(SystemClock.elapsedRealtime());//设置起始时间
        chronometer.setFormat("已用时间：%s");//设置输出格式
        chronometer.start();
        chronometer.setOnChronometerTickListener(new chronometerListener());
        reset.setOnClickListener(new restOnClickListener());
    }

    class restOnClickListener implements View.OnClickListener{
        public void onClick(View v){
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            reset.setVisibility(View.GONE);
        }
    }

    class chronometerListener implements Chronometer.OnChronometerTickListener{
        public void onChronometerTick(Chronometer chronometer){
            if(SystemClock.elapsedRealtime() - chronometer.getBase() >= 10000){
                chronometer.stop();
                reset.setVisibility(View.VISIBLE);
            }
        }
    }
}
